<template>
    <el-dialog v-model="isDrawer" title="编辑直播" :before-close="close">
        <el-form :model="editLive" ref="formRef" :rules="rule">
            <el-form-item label="直播标题" prop="liveName">
                <el-input v-model="editLive.liveName" placeholder="请输入直播标题"></el-input>
            </el-form-item>
            <el-form-item label="直播开始时间" prop="liveTime">
                <el-date-picker v-model="editLive.liveTime" placeholder="请选择直播开始时间" type="datetime"
                    :default-time="defaultTime" :disabled-date="disabledFn"></el-date-picker>
            </el-form-item>
            <el-form-item label="直播时长" prop="liveDuration">
                <el-input v-model="editLive.liveDuration" placeholder="请输入直播时长(分钟)"></el-input>
            </el-form-item>
            <el-form-item label="讲师" prop="teacher">
                <el-select v-model="editLive.teacher" placeholder="请选择讲师">
                    <el-option :label="'大炮'" :value="'大炮'" />
                    <el-option :label="'李旋飞'" :value="'李旋飞'" />
                </el-select>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="close">取消</el-button>
            <el-button type="primary" @click="commit">保存</el-button>
        </template>
    </el-dialog>
</template>

<script setup>
import { defineExpose, ref,defineEmits } from 'vue'
import { reviseLive } from '@/apis/live'
import { quiFn } from '@/hooks/quiFn'; // 弹窗插件
import { ElMessage } from 'element-plus';
// 最后提交的数据
const editLive = ref({
    liveName: '',
    liveTime: '',
    liveDuration: '',
    teacher: '',
    liveListId:'' // 外层数组id
})
// 表单规则
const rule = ref({
    liveName: [{ required: true, message: '直播标题是必填项', trigger: 'bulr' }],
    liveDuration: [{ required: true, message: '直播开始时间是必填项', trigger: 'bulr' }],
    teacher: [{ required: true, message: '直播时长是必填项', trigger: 'bulr' }],
    liveTime: [{ required: true, message: '讲师必填项', trigger: 'bulr' }],
})
const formRef = ref({})
const defaultTime = new Date(2000, 1, 1, 20, 30, 0)
const isDrawer = ref(false)
const open = (row,liveListId) => {
    console.log(row);
    for (const key in row){
        editLive.value[key] = row[key]
    }

    editLive.value.liveListId = liveListId
    isDrawer.value = true
}
const close = () => {
    quiFn({}, () => {
        isDrawer.value = false
        formRef.value.resetFields()
        formRef.value.clearValidate()
    })
}

// 今天之前的日期不能被选中
const disabledFn = (value) => {
    const date = new Date()
    return date >= value
}


const emit = defineEmits(['change'])
const commit = ()=>{
    formRef.value.validate(async (value) => {
        if (value) {
            const { data } = await reviseLive({
                liveName: editLive.value.liveName,
                liveTime: editLive.value.liveTime,
                liveDuration: editLive.value.liveDuration,
                teacher: editLive.value.teacher,
                _id:editLive.value.liveListId, // 外层数组id
                id:editLive.value._id // 数组项item的id
            })
            if (!data.code) {
                ElMessage.success('修改成功')
                // 触发父组件中的自定义事件，让父组件重新获取数据
                emit('change')
                // 关闭对话框
                isDrawer.value = false;
            }

        } else {
            ElMessage.error('参数有误')
        }
    })
}
defineExpose({
    open
})
</script>


<style lang="scss" scoped>
:deep(.el-derwer_body) {
    padding-top: 0;
}

.header {
    position: absolute;
    box-sizing: border-box;
    width: 92%;
    top: 70px;
    border-bottom: 1px solid #eee;
    padding-bottom: 20px;
    background-color: #fff;
    z-index: 100;

    h3 {
        font-size: 15px;
        color: #000;
    }

    p {
        font-size: 13px;
        color: #666;
        margin-top: 20px;
        margin-bottom: 20px;

        span {
            color: #ccc;
        }
    }
}

.live-list {
    margin-top: 130px;

    .live-item {
        position: relative;
        padding: 30px 60px;

        .live-content {
            h5 {
                font-size: 14px;
            }

            div {
                margin-top: 15px;
                font-size: 12px;
                color: #bfbfbf;

                span {
                    margin-right: 25px;
                }
            }


            .delete {
                cursor: pointer;
                font-size: 20px;
                display: block;
                position: absolute;
                left: 50%;
                top: 50px;
                margin-left: 200px;
                color: #e37777;
            }
        }
    }
}

:deep(.el-form-item_label) {
    width: 130px;
}

:deep(.el-input) {
    width: 80%;
}

.tag {
    position: absolute;
    left: 12px;
    top: 50px;
    width: 40px;
    height: 20px;
    border: 1px solid #eee;
    text-align: center;
    line-height: 20px;
    font-size: 12px;
    background-color: #fafafa;
    border-radius: 5px;
}
</style>